import cTitle from 'components/title';
import { scrollMixin } from "utils/mixin";
import diyBackGround from "../balance/componet/diyBackGround.vue";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      toi: this.fun.getKeyByI(),
      selected: '',
      all: [],
      //收入积分
      income: [],
      //消费积分
      out: [],
      recordsList: [],

      integral: window.localStorage.integral,

      // new
      statusValue: 0,
      selectShow: false,
      selectType: 0,
      record_list: [],
      money_arr: [],
      currentDate: new Date(),
      maxDate: new Date(),
      show: false,
      postDate: "",
      noneShow: false,
      selectTimeDate: "",
      // 业务类型数组
      source_comment:[],
      selectTapInd: 0,

      //more
      isLoadMore: true,
      page: 1,
      total_page: 0,
    };
  },
  methods: {
    // initData() {
    //   this.selected = "";
    //   this.isLoadMore = true;
    //   this.page = 1;
    //   this.total_page = 0;
    //   this.recordsList = [];
    // },
    // swichTabTItem() {
    //   this.getIntegralInfo(this.selected);
    // },
    expand(item) {
      this.$forceUpdate();
      this.$router.push(this.fun.getUrl("integraldetail", { id: item.id }));
    },
    getData(type) {
      let json = {
        status: this.statusValue == 2 ? '-1' : this.statusValue,
        month: this.postDate,
        mode: this.selectType
      };
      $http
        .post("finance.point-info", json, "加载中")
        .then(response => {
          if (response.result === 1) {
            this.isLoadMore = true;
            this.noneShow = false;
            this.total_page = response.data.record_list.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.money_arr = response.data.point_arr;
            this.record_list = response.data.record_list;
            if(response.data.source_comment){
              this.source_comment = response.data.source_comment;
              this.source_comment[0] = '变动类型'
            }
            // let arr = ['业务类型'];
            // this.source_comment.unshift(arr);
          } else {
            this.noneShow = true;
            this.isLoadMore = false; // 防止多次请求分页数据
            this.record_list = [];
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    //获取更多数据
    getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page += 1;
        let json = {
          status: this.statusValue == 2 ? '-1' : this.statusValue,
          month: this.postDate,
          mode: this.selectType,
          page: this.page
        };
        $http.post("finance.point-info", json).then(response => {
          if (response.result === 1) {
            var nextPageData = response.data.record_list.data;
            this.money_arr = Object.assign(this.money_arr, response.data.point_arr);
            for (const key in nextPageData) {
              if (this.record_list.data[key]) {
                this.$set(this.record_list.data, key, [...this.record_list.data[key], ...nextPageData[key]]);
              } else {
                this.$set(this.record_list.data, key, nextPageData[key]);
              }
            }
            this.isLoadMore = true;
            console.log(this.record_list.data);
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
          }
        });
      }
    },
    detailsTo(info) {
      this.$router.push(this.fun.getUrl("details", { item: info }));
    },
    formatter(type, val) {
      if (type === "year") {
        return `${val}年`;
      } else if (type === "month") {
        return `${val}月`;
      }
      return val;
    },
    selectStatus(key) {
      this.init();
      this.statusValue = key;
      this.getData();
    },
    selectTap(key) {
      this.init();
      this.selectType = key;
      this.selectTapInd = key;
      this.getData();
    },
    pickConfirm(e) {
      this.postDate = this.timestampToTime(Date.parse(e));
      this.show = false;
      this.init();
      this.getData();
    },
    init() {
      this.isLoadMore = true;
      this.page = 1;
      this.total_page = 0;
      this.record_list = [];
      this.money_arr = [];
      this.selectShow = false;
    },
    //将时间戳转换成正常时间格式
    timestampToTime: function (timestamp, key) {
      if (!Number(timestamp) || timestamp == "") {
        return "";
      }
      let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + "-";
      let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
      let D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      return Y + M + D;
    },
  },
  activated() {
    this.init();
    this.toi = this.fun.getKeyByI();
    this.getData();
    this.fun.setWXTitle(this.integral + '列表');
  },
  components: { cTitle, diyBackGround }
};